//
//  CallingNoteView.m
//  cloudCall
//
//  Created by zhang on 2021/11/2.
//

#import "CallingNoteView.h"
#import <CloudCallBaseFrame/CommonFrame.h>
#import "QTBClientCallCore.h"


@interface CallingNoteView ()

@property (nonatomic, strong)UILabel *noteLabel;
/**图片*/
@property (nonatomic, strong)UIImageView *imageView;
/// 背景
@property (nonatomic, strong)UIView *backView;

@property (nonatomic,assign)LCCallingType callingType;



@end


@implementation CallingNoteView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(kScreenWidth-60, kScreenHeight-AutoBottomHeight-130, 60, 60)];
    if (self) {
        [self setUpUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidEnd) name:CallingDidEndNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidError) name:CallingDidErrorNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidConnect) name:CallingDidConnectNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingTimeChange:) name:CallingTimeChangedNotification object:nil];
        
    }
    return self;
}

-(void)setUpUI{
    UIView *backView = [[UIView alloc] init];
    self.backView = backView;
    backView.frame = self.bounds;
    [self addSubview:backView];
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:(CGSize){10.0}];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bezierPath.CGPath;
    backView.layer.mask = shapeLayer;
    backView.backgroundColor = [UIColor whiteColor];
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset =  CGSizeZero;
    self.layer.shadowRadius = 4;
    self.layer.shadowOpacity = 0.15;
    UILongPressGestureRecognizer *panGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClicked:)];
    [self addGestureRecognizer:panGesture];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tap];
    self.imageView = [[UIImageView alloc] init];
    [self addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(8);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    self.noteLabel = [[UILabel alloc] init];
    self.noteLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.noteLabel];
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.offset(-6);
    }];
}

-(void)tapAction{
    if (self.clickBlock) {
        self.clickBlock();
    }
    [self removeFromSuperview];
}

-(void)showView{
    //    self.frame = CGRectMake(kScreenWidth-60, kScreenHeight-AutoBottomHeight-130, 60, 60);
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    if ([QTBClientCallCore shareCallCore].currentCall.status == QTBCallSessionStatusConnect) {
        self.callingType = CONNECTING;
    } else {
        self.callingType = AWAITING;
    }
}

-(void)setCallingType:(LCCallingType)callingType {
    _callingType = callingType;
    if (callingType == AWAITING) {
        self.backView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        self.noteLabel.textColor = [UIColor colorWithHexString:@"#52C41A"];
        if ([QTBClientCallCore shareCallCore].currentCall.outbound) {
            self.noteLabel.text = @"呼叫中";
        } else {
            self.noteLabel.text = @"等待接听";
        }
        self.imageView.image = [UIImage imageNamed:@"ImageSource.bundle/calling_waiting@3x"];
    } else {
        self.backView.backgroundColor = [UIColor colorWithHexString:@"#52C41A"];
        self.noteLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        self.imageView.image = [UIImage imageNamed:@"ImageSource.bundle/calling_connect@3x"];
    }
}

-(void)callingTimeChange:(NSNotification *)sender{
    if (sender.object && [sender.object isKindOfClass:[NSString class]]) {
        NSString *time = sender.object;
        self.noteLabel.text = time;
    }
}

-(void)callingDidEnd{
    [self removeFromSuperview];
}


-(void)callingDidError{
    [self removeFromSuperview];
}


-(void)callingDidConnect{
    self.callingType = CONNECTING;
}

-(void)longPressClicked:(UILongPressGestureRecognizer *)sender{
    UIGestureRecognizerState state = sender.state;
    CGPoint touchPoint = [sender locationInView:self.superview];
    if (state == UIGestureRecognizerStateBegan ) {
        [self shakeView];
    } else if (state == UIGestureRecognizerStateEnded) {
        if (touchPoint.x > kScreenWidth/2) {
            self.center = CGPointMake(kScreenWidth-30, touchPoint.y);
            UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:(CGSize){10.0}];
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            shapeLayer.path = bezierPath.CGPath;
            self.backView.layer.mask = shapeLayer;
        } else {
            self.center = CGPointMake(30, touchPoint.y);
            UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:(CGSize){10.0}];
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            shapeLayer.path = bezierPath.CGPath;
            self.backView.layer.mask = shapeLayer;
        }
    } else {
        self.center = touchPoint;
    }
}


- (void)shakeView{
    //添加摇动动画
    CABasicAnimation *shakeAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    shakeAnimation.duration= 0.3;
    //设置摇动幅度
    shakeAnimation.fromValue=[NSNumber numberWithFloat:-0.1];
    shakeAnimation.toValue=[NSNumber numberWithFloat:0.1];
    //设置自动倒带
    shakeAnimation.autoreverses=YES;
    //设置完成后自动移动动画
    shakeAnimation.removedOnCompletion=YES;
    shakeAnimation.repeatCount=1;
    [self.layer addAnimation:shakeAnimation forKey:@"Let me Shake"];
}



@end
